<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>loremipsum.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_editor_plugins_loremipsum.js.html">loremipsum.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview A plugin that fills the field with lorem ipsum text when it&#39;s
<a name="line17"></a> * empty and does not have the focus. Applies to both editable and uneditable
<a name="line18"></a> * fields.
<a name="line19"></a> *
<a name="line20"></a> */
<a name="line21"></a>
<a name="line22"></a>goog.provide(&#39;goog.editor.plugins.LoremIpsum&#39;);
<a name="line23"></a>
<a name="line24"></a>goog.require(&#39;goog.asserts&#39;);
<a name="line25"></a>goog.require(&#39;goog.dom&#39;);
<a name="line26"></a>goog.require(&#39;goog.editor.Command&#39;);
<a name="line27"></a>goog.require(&#39;goog.editor.Plugin&#39;);
<a name="line28"></a>goog.require(&#39;goog.editor.node&#39;);
<a name="line29"></a>goog.require(&#39;goog.functions&#39;);
<a name="line30"></a>
<a name="line31"></a>
<a name="line32"></a>
<a name="line33"></a>/**
<a name="line34"></a> * A plugin that manages lorem ipsum state of editable fields.
<a name="line35"></a> * @param {string} message The lorem ipsum message.
<a name="line36"></a> * @constructor
<a name="line37"></a> * @extends {goog.editor.Plugin}
<a name="line38"></a> */
<a name="line39"></a>goog.editor.plugins.LoremIpsum = function(message) {
<a name="line40"></a>  goog.editor.Plugin.call(this);
<a name="line41"></a>
<a name="line42"></a>  /**
<a name="line43"></a>   * The lorem ipsum message.
<a name="line44"></a>   * @type {string}
<a name="line45"></a>   * @private
<a name="line46"></a>   */
<a name="line47"></a>  this.message_ = message;
<a name="line48"></a>};
<a name="line49"></a>goog.inherits(goog.editor.plugins.LoremIpsum, goog.editor.Plugin);
<a name="line50"></a>
<a name="line51"></a>
<a name="line52"></a>/** @override */
<a name="line53"></a>goog.editor.plugins.LoremIpsum.prototype.getTrogClassId =
<a name="line54"></a>    goog.functions.constant(&#39;LoremIpsum&#39;);
<a name="line55"></a>
<a name="line56"></a>
<a name="line57"></a>/** @override */
<a name="line58"></a>goog.editor.plugins.LoremIpsum.prototype.activeOnUneditableFields =
<a name="line59"></a>    goog.functions.TRUE;
<a name="line60"></a>
<a name="line61"></a>
<a name="line62"></a>/**
<a name="line63"></a> * Whether the field is currently filled with lorem ipsum text.
<a name="line64"></a> * @type {boolean}
<a name="line65"></a> * @private
<a name="line66"></a> */
<a name="line67"></a>goog.editor.plugins.LoremIpsum.prototype.usingLorem_ = false;
<a name="line68"></a>
<a name="line69"></a>
<a name="line70"></a>/**
<a name="line71"></a> * Handles queryCommandValue.
<a name="line72"></a> * @param {string} command The command to query.
<a name="line73"></a> * @return {boolean} The result.
<a name="line74"></a> * @override
<a name="line75"></a> */
<a name="line76"></a>goog.editor.plugins.LoremIpsum.prototype.queryCommandValue = function(command) {
<a name="line77"></a>  return command == goog.editor.Command.USING_LOREM &amp;&amp; this.usingLorem_;
<a name="line78"></a>};
<a name="line79"></a>
<a name="line80"></a>
<a name="line81"></a>/**
<a name="line82"></a> * Handles execCommand.
<a name="line83"></a> * @param {string} command The command to execute.
<a name="line84"></a> *     Should be CLEAR_LOREM or UPDATE_LOREM.
<a name="line85"></a> * @param {*=} opt_placeCursor Whether to place the cursor in the field
<a name="line86"></a> *     after clearing lorem. Should be a boolean.
<a name="line87"></a> * @override
<a name="line88"></a> */
<a name="line89"></a>goog.editor.plugins.LoremIpsum.prototype.execCommand = function(command,
<a name="line90"></a>    opt_placeCursor) {
<a name="line91"></a>  if (command == goog.editor.Command.CLEAR_LOREM) {
<a name="line92"></a>    this.clearLorem_(!!opt_placeCursor);
<a name="line93"></a>  } else if (command == goog.editor.Command.UPDATE_LOREM) {
<a name="line94"></a>    this.updateLorem_();
<a name="line95"></a>  }
<a name="line96"></a>};
<a name="line97"></a>
<a name="line98"></a>
<a name="line99"></a>/** @override */
<a name="line100"></a>goog.editor.plugins.LoremIpsum.prototype.isSupportedCommand =
<a name="line101"></a>    function(command) {
<a name="line102"></a>  return command == goog.editor.Command.CLEAR_LOREM ||
<a name="line103"></a>      command == goog.editor.Command.UPDATE_LOREM ||
<a name="line104"></a>      command == goog.editor.Command.USING_LOREM;
<a name="line105"></a>};
<a name="line106"></a>
<a name="line107"></a>
<a name="line108"></a>/**
<a name="line109"></a> * Set the lorem ipsum text in a goog.editor.Field if needed.
<a name="line110"></a> * @private
<a name="line111"></a> */
<a name="line112"></a>goog.editor.plugins.LoremIpsum.prototype.updateLorem_ = function() {
<a name="line113"></a>  // Try to apply lorem ipsum if:
<a name="line114"></a>  // 1) We have lorem ipsum text
<a name="line115"></a>  // 2) There&#39;s not a dialog open, as that screws
<a name="line116"></a>  //    with the dialog&#39;s ability to properly restore the selection
<a name="line117"></a>  //    on dialog close (since the DOM nodes would get clobbered in FF)
<a name="line118"></a>  // 3) We&#39;re not using lorem already
<a name="line119"></a>  // 4) The field is not currently active (doesn&#39;t have focus).
<a name="line120"></a>  var fieldObj = this.getFieldObject();
<a name="line121"></a>  if (!this.usingLorem_ &amp;&amp;
<a name="line122"></a>      !fieldObj.inModalMode() &amp;&amp;
<a name="line123"></a>      goog.editor.Field.getActiveFieldId() != fieldObj.id) {
<a name="line124"></a>    var field = fieldObj.getElement();
<a name="line125"></a>    if (!field) {
<a name="line126"></a>      // Fallback on the original element. This is needed by
<a name="line127"></a>      // fields managed by click-to-edit.
<a name="line128"></a>      field = fieldObj.getOriginalElement();
<a name="line129"></a>    }
<a name="line130"></a>
<a name="line131"></a>    goog.asserts.assert(field);
<a name="line132"></a>    if (goog.editor.node.isEmpty(field)) {
<a name="line133"></a>      this.usingLorem_ = true;
<a name="line134"></a>
<a name="line135"></a>      // Save the old font style so it can be restored when we
<a name="line136"></a>      // clear the lorem ipsum style.
<a name="line137"></a>      this.oldFontStyle_ = field.style.fontStyle;
<a name="line138"></a>      field.style.fontStyle = &#39;italic&#39;;
<a name="line139"></a>      fieldObj.setHtml(true, this.message_, true);
<a name="line140"></a>    }
<a name="line141"></a>  }
<a name="line142"></a>};
<a name="line143"></a>
<a name="line144"></a>
<a name="line145"></a>/**
<a name="line146"></a> * Clear an EditableField&#39;s lorem ipsum and put in initial text if needed.
<a name="line147"></a> *
<a name="line148"></a> * If using click-to-edit mode (where Trogedit manages whether the field
<a name="line149"></a> * is editable), this works for both editable and uneditable fields.
<a name="line150"></a> *
<a name="line151"></a> * TODO(user): Is this really necessary? See TODO below.
<a name="line152"></a> * @param {boolean=} opt_placeCursor Whether to place the cursor in the field
<a name="line153"></a> *     after clearing lorem.
<a name="line154"></a> * @private
<a name="line155"></a> */
<a name="line156"></a>goog.editor.plugins.LoremIpsum.prototype.clearLorem_ = function(
<a name="line157"></a>    opt_placeCursor) {
<a name="line158"></a>  // Don&#39;t mess with lorem state when a dialog is open as that screws
<a name="line159"></a>  // with the dialog&#39;s ability to properly restore the selection
<a name="line160"></a>  // on dialog close (since the DOM nodes would get clobbered)
<a name="line161"></a>  var fieldObj = this.getFieldObject();
<a name="line162"></a>  if (this.usingLorem_ &amp;&amp; !fieldObj.inModalMode()) {
<a name="line163"></a>    var field = fieldObj.getElement();
<a name="line164"></a>    if (!field) {
<a name="line165"></a>      // Fallback on the original element. This is needed by
<a name="line166"></a>      // fields managed by click-to-edit.
<a name="line167"></a>      field = fieldObj.getOriginalElement();
<a name="line168"></a>    }
<a name="line169"></a>
<a name="line170"></a>    goog.asserts.assert(field);
<a name="line171"></a>    this.usingLorem_ = false;
<a name="line172"></a>    field.style.fontStyle = this.oldFontStyle_;
<a name="line173"></a>    fieldObj.setHtml(true, null, true);
<a name="line174"></a>
<a name="line175"></a>    // TODO(nicksantos): I&#39;m pretty sure that this is a hack, but talk to
<a name="line176"></a>    // Julie about why this is necessary and what to do with it. Really,
<a name="line177"></a>    // we need to figure out where it&#39;s necessary and remove it where it&#39;s
<a name="line178"></a>    // not. Safari never places the cursor on its own willpower.
<a name="line179"></a>    if (opt_placeCursor &amp;&amp; fieldObj.isLoaded()) {
<a name="line180"></a>      if (goog.userAgent.WEBKIT) {
<a name="line181"></a>        goog.dom.getOwnerDocument(fieldObj.getElement()).body.focus();
<a name="line182"></a>        fieldObj.focusAndPlaceCursorAtStart();
<a name="line183"></a>      } else if (goog.userAgent.OPERA) {
<a name="line184"></a>        fieldObj.placeCursorAtStart();
<a name="line185"></a>      }
<a name="line186"></a>    }
<a name="line187"></a>  }
<a name="line188"></a>};
</pre>


</body>
</html>
